package com.tfj.mvp.tfj.home.shop;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.tfj.R;
import com.tfj.mvp.base.BaseActivity;
import com.tfj.mvp.tfj.home.shop.adapter.JingjiAdapter;
import com.tfj.mvp.tfj.home.shop.bean.ShopJingJIBean;
import com.tfj.mvp.tfj.home.shop.bean.ShopShowBean;
import com.tfj.utils.AntiShakeUtils;
import com.tfj.utils.SysUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VShopDetailAcitivity extends BaseActivity {
    @BindView(R.id.recyclew_jingji)
    RecyclerView recyclewJingji;
    @BindView(R.id.recyclew_shop_img)
    RecyclerView recyclewShopImg;
    @BindView(R.id.nestView)
    NestedScrollView nestView;
    @BindView(R.id.ll_bar)
    LinearLayout llBar;
    @BindView(R.id.back_titlebar_iv)
    ImageView backTitlebarIv;
    @BindView(R.id.rl_jingji)
    RelativeLayout rlJingji;
    private int width = 0;

    @Override
    protected void initView() {
        super.initView();
        setTitleText("商家详情");
        width = SysUtils.getWidth(this) / 2 - SysUtils.dip2px(this, 36) / 2;
        setNestBar();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclewJingji.setLayoutManager(linearLayoutManager);
        JingjiAdapter jingjiAdapter = new JingjiAdapter(this);
        recyclewJingji.setAdapter(jingjiAdapter);
        List<ShopJingJIBean> shopJingJIBeanList = new ArrayList<>();
        shopJingJIBeanList.add(new ShopJingJIBean());
        shopJingJIBeanList.add(new ShopJingJIBean());
        shopJingJIBeanList.add(new ShopJingJIBean());
        shopJingJIBeanList.add(new ShopJingJIBean());
        shopJingJIBeanList.add(new ShopJingJIBean());
        shopJingJIBeanList.add(new ShopJingJIBean());
        shopJingJIBeanList.add(new ShopJingJIBean());
        jingjiAdapter.replaceData(shopJingJIBeanList);
        //图片
        LinearLayoutManager linearLayoutManager_image = new LinearLayoutManager(this);
        linearLayoutManager_image.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclewShopImg.setLayoutManager(linearLayoutManager_image);
        recyclewShopImg.setAdapter(quickAdapter_image);
        List<ShopShowBean> shopShowBeans = new ArrayList<>();
        shopShowBeans.add(new ShopShowBean());
        shopShowBeans.add(new ShopShowBean());
        shopShowBeans.add(new ShopShowBean());
        shopShowBeans.add(new ShopShowBean());
        shopShowBeans.add(new ShopShowBean());
        shopShowBeans.add(new ShopShowBean());
        quickAdapter_image.replaceData(shopShowBeans);
    }

    @Override
    public int setContentViewId() {
        return R.layout.layout_shop_detail;
    }

    @Override
    public void createPresenter() {

    }

    private void setNestBar() {
        final int height = llBar.getLayoutParams().height;
        nestView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            // 将透明度声明成局部变量用于判断
            int alpha = 0;
            float scale = 0;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                if (scrollY <= height) {
                    scale = (float) scrollY / height;
                    alpha = (int) (255 * scale);
                    // 随着滑动距离改变透明度
                    // Log.e("al=","="+alpha);
                    llBar.setBackgroundColor(Color.argb(alpha, 255, 255, 255));
                } else {
                    if (alpha < 255) {
                        // 防止频繁重复设置相同的值影响性能
                        alpha = 255;
                        llBar.setBackgroundColor(Color.argb(alpha, 255, 255, 255));
                    }
                }
            }

        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private QuickAdapter_Image quickAdapter_image = new QuickAdapter_Image();

    @OnClick({R.id.back_titlebar_iv, R.id.rl_jingji})
    public void onViewClicked(View view) {
        if (AntiShakeUtils.isInvalidClick(view)) {
            return;
        }
        switch (view.getId()) {
            case R.id.back_titlebar_iv:
                this.finish();
                break;
            case R.id.rl_jingji:
                startActivity(VJingjiListActivity.class);
                break;
        }
    }

    public class QuickAdapter_Image extends BaseQuickAdapter<ShopShowBean, BaseViewHolder> {
        public QuickAdapter_Image() {
            super(R.layout.item_image_detail_100);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final ShopShowBean item) {
            ImageView imageView = viewHolder.getView(R.id.imageView);
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.width = width;
            imageView.setLayoutParams(layoutParams);

        }
    }
}
